<template>
  <div class="_wz_position">
    <Card dis-hover :bordered="true" style="height: 100%;">
      <template #title>列表</template>
      <template #extra>
        <Button type="primary" @click="addTableData" style="position: relative;top: -8px;">添加</Button>
        <Button type="primary" @click="loadData" style="position: relative;top: -8px;margin: 0 0 0 10px;">刷新</Button>
      </template>

      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>


    <Drawer :title="formTitle" v-model="formOperation" width="720" :draggable="true" :mask-closable="false" :styles="formOperationStyles">
      <positionAdd v-if="formOperation" :selectData="selectData" @closeFun="closeFun"></positionAdd>
    </Drawer>
  </div>
</template>
<script>


import {
  getCurrentInstance,
  inject,
  nextTick,
  reactive,
  toRefs,
  toRef,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
  ref,
  defineComponent,
  toRaw,
  watch,
  resolveComponent
} from 'vue'
import { useRouter,useRoute } from "vue-router";

import { useMainStore } from '../../store'  //pinia 使用 类似 vuex
import { storeToRefs } from 'pinia'//pinia 使用 类似 vuex
const mainStore = useMainStore() //pinia 使用 类似 vuex


import wzTbale from "../../components/table/index.vue";
import positionAdd from "../sys/positionAdd.vue";
//reactive
export default defineComponent({
  name: 'position',//组件名称
  components: {
    wzTbale,
    positionAdd,
  },//组件引用
  setup (props,context) {


    const apiUrl = inject('$apiUrl'); // 变量全局化 后台地址
    const $axios = inject('$axiosInterceptor');// 变量全局化 axios
    const $ViewUIPlus = inject('$ViewUIPlus');//引用全局组件
    const router = useRouter();//路由跳转使用
    const $common = inject('$common');//自己封装的方法


    const state = reactive({
      formOperationStyles: {//表单左侧移出css
        height: 'calc(100% - 55px)',
        overflow: 'inherit',
        paddingBottom: '53px',
        position: 'static'
      },
      formTitle:'',
      selectData:{},
      formOperation:false,

      tableData: {
        url: '',//请求地址
        method: 'post',//请求方式
        where: {
          // alarmDeptUuid:props.selectData.uuid
        },//搜索条件
        rowKey:'uuid',
        height: '100%',//表格高度 auto
        page: false,//分页是否使用
        columns: [//表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          {title: '部门名称', minWidth: 160, key: 'inOrgName', tooltip: true,tree: true,render: (h, params) => {
              return h('span', { style: { "margin": "0 0 0 10px"}}, `${params.row.inOrgName}`)
            }},
          {title: '负责人', minWidth: 100, key: 'managerUserName',tooltip:true,resizable: true,},
          {title: '创建时间', minWidth: 160, key: 'createTime',tooltip:true,resizable: true,},
          {title: '修改时间', minWidth: 160, key: 'updateTime',tooltip:true,resizable: true,},
          {
            title: '操作', width: 120, fixed: 'right', render: (h, params) => {

              var operationButtons = [];
              operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    onClick: () => {
                      state.selectData = JSON.parse(JSON.stringify(params.row));
                      state.formTitle = '修改';
                      state.formOperation = true;
                    }
                  }, "修改")
              )

              operationButtons.push(h(resolveComponent('Divider'), {type: 'vertical',}, ''))
              operationButtons.push(
                  h('span', { style: {"color": "#2d8cf0", "cursor": "pointer"},
                    onClick:async () => {

                      $ViewUIPlus.Modal.confirm({
                        title: '删除提醒',
                        content: '确定要删除选中的数据？',
                        onOk: () => {
                          $ViewUIPlus.Spin.show()
                          $axios.post(apiUrl.coreInOrgCoreInOrgDelete,{uuid:params.row.uuid}).then((res) => {
                            $ViewUIPlus.Spin.hide()
                            if (!!res) {
                              $ViewUIPlus.Message.success('删除成功');
                              loadData();
                            }
                          }).catch((err) => {
                            console.log("失败", err)
                          })
                        },
                        onCancel: () => {
                          //点击取消
                        }
                      });
                    }
                  }, '删除')
              )
              return operationButtons;
            }
          },

        ],
        pagingParameters:{// 分页参数
          page:'pageNo',
          limit:'pageSize'
        },
        format: {//返回数据格式
          list: ['data'],//数据list
          count: ['count'],//数据总量
        },
        dataList:[],
      },
    });


    //table添加
    const addTableData = ()=> {
      state.formTitle = "添加"
      state.selectData = {}
      state.formOperation = true
    }

    //关闭 弹出框刷新列表
    const closeFun =  (data)=> {
      if (data) {//添加修改 刷新表单
        loadData();
      }
      state.formOperation = false
    }

    //加载表格
    const loadData = ()=>{
      $axios.post(apiUrl.coreInOrgCoreInOrgList,{}).then((res) => {
        if (!!res) {
          res.data.data.map((k,i)=>{
            k._showChildren = true;
          })
          let tableDataList = $common.toTreeData(res.data.data,{
            id: 'id',
            parendId: 'parentId',
            name: 'inOrgName',
            rootId: 0
          })

          state.tableData.dataList = tableDataList;
        }
      }).catch((err) => {
        console.log("失败", err)
      })
    }


    // 其他的生命周期
    onBeforeMount (() => {//beforeMount挂载前状态 // console.log("App ===> 相当于 vue2.x 中 beforeMount")

    })
    onMounted (() => {//mounted 挂载结束状态  // console.log("App ===> 相当于 vue2.x 中 mounted")
      loadData();
    })

    // 注意，onBeforeUpdate 和 onUpdated 里面不要修改值
    onBeforeUpdate (() => {//beforeUpdate 更新前状态 // console.log("App ===> 相当于 vue2.x 中 beforeUpdate")

    })

    onUpdated (() => {//updated 更新完成状态 // console.log("App ===> 相当于 vue2.x 中 updated")

    })

    onBeforeUnmount (() => {//beforeDestroy 销毁前状态  // console.log("App ===> 相当于 vue2.x 中 beforeDestroy")
    })

    onUnmounted (() => {//destroyed 销毁完成状态  // console.log("App ===> 相当于 vue2.x 中 destroyed")
    })

    return {
      ...toRefs(state),
      addTableData,//添加
      loadData,//列表加载刷新
      closeFun,//关闭弹层 刷新
    }
  }
})


</script>
<style lang="less">
._wz_position{
  padding: 10px;
  height: 100%;
  display: flex;
  flex-direction: column;

  .ivu-card-body {
    height: calc(100% - 51px);
  }
}
</style>
